<?php

namespace App\Http\Controllers\V1\Api;

use App\Http\Requests\Api\UserVoteServer;
use App\Models\V1\Config;
use App\Models\V1\Preparatory;
use App\Models\V1\Users;
use App\Models\V1\UserVote;
use App\Models\V1\Vote;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;



class VoteController extends BaseController
{

    /**
     * 投票列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function List(Request $request)
    {
        $user = auth('box')->user();
        $query = Vote::query();
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'created_at');
        $query->where('state', 1);
        $query->where('preparatory_id', $user['preparatory_id']);
        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);

        $integral = Config::where('id', 2)->value('config_value');//投票送米米
        $data->each(function ($item) use ($integral) {
            $item->integral = $integral;
        });

        return ajax_output(200, "成功", $data);
    }


    /**
     * 用户投票
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Add(UserVoteServer $request)
    {
        $request->validate('v1.create');
        $id = auth('box')->id();
        $vote_id = $request->input('vote_id');
        $pac = $request->input('pac');//0 反  1赞成
        $user_vote_model = new UserVote();
        $da = $user_vote_model->where('user_id', $id)->where('vote_id', $vote_id)->exists();
        $integral = Config::where('id', 2)->value('config_value');//投票送米米

        if ($da) {
            return ajax_output(300, "不能重复投票");
        }
        try {
            DB::beginTransaction();
            $user_vote_model->create([
                'user_id' => $id,
                'vote_id' => $vote_id,
                'pac' => $pac,
            ]);
            if ($pac == 1) {
                Users::where('id',$id)->increment('integral',$integral);
                Vote::where('id', $vote_id)->increment('voter_turnout');
                Vote::where('id', $vote_id)->increment('a_vote');
            } else {
                Users::where('id',$id)->increment('integral',$integral);
                Vote::where('id', $vote_id)->increment('voter_turnout');
                Vote::where('id', $vote_id)->increment('n_vote');
            }
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
        return ajax_output(200, "成功");
    }


    /**
     * 我的投票(投票列表)
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function MyVote(Request $request)
    {
        $user = auth('box')->user();
        $query = Vote::query();
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'created_at');
        $vote_state = $request->input('vote_state',);//1赞成 2反对 3未投票 不传为全部接投票
        $query->where('state', 1);
        $query->where('preparatory_id', $user['preparatory_id']);
        $query->orderBy($sort, $order);
        $integral = Config::where('id', 2)->value('config_value');//投票送米米
        if (!$vote_state) {

            $currentTime = now(); // 获取当前时间
            $query->where('end_time',  '>=', $currentTime);
            $data = $query->paginate($pageSize);

            // 使用map方法来增加一个新的字段
            $data->each(function ($item) use ($user,$integral) { // 使用use导入$user到闭包中
                $re = UserVote::where('user_id', $user->id)->where('vote_id', $item->id)->first();
                $item->integral = $integral;
                if ($re) {
                    if ($re['pac'] == 1) {
                        $item->is_vote = 1;//赞成票
                    } else {
                        $item->is_vote = 2;//反对票
                    }
                } else {
                    $item->is_vote = 3;//未投票
                }
            });
        } else {
            if ($vote_state == 1 || $vote_state == 2) {
                $pre = UserVote::where('user_id', $user['id'])->where('pac', $vote_state)->pluck('vote_id')->toArray();
                $query->whereIn('id', $pre);
                $data = $query->paginate($pageSize);
                $data->each(function ($item) use ($vote_state,$integral) { // 使用use导入$user到闭包中
                    $item->is_vote = $vote_state;
                    $item->integral = $integral;
                });
            } else {
                $pre = UserVote::where('user_id', $user['id'])->pluck('vote_id')->toArray();
                $query->whereNotIn('id', $pre);

                $currentTime = now(); // 获取当前时间
                $query->where('end_time',  '>=', $currentTime);

                $data = $query->paginate($pageSize);
                $data->each(function ($item) use ($vote_state,$integral) { // 使用use导入$user到闭包中
                    $item->is_vote = 3;
                    $item->integral = $integral;
                });
            }

        }
        return ajax_output(200, "成功", $data);
    }


}
